<?php
!defined('P_W') && exit('Forbidden');
require_once MODE . '/base/basehelper.php';
/**
 * 出租相关的helper
 * @author suqian
 */
class PW_HouseHireHelper extends BaseHelper {
	var $baseUrl = BASEURL;
	/**
	 * 出租房module数据组装
	 *@param object $module 出租房module
	 *@return object;
	 */
	function buildHouseHireModule($module){
		global $db_windpost;
		if(!is_object($module)){
			Error::addError("{$module}不是一个对象");
		}
		require (R_P . 'require/bbscode.php');
		$viewUtility= $this->getHouseViewUtility();
		$descrip = $viewUtility->getDescrip($module->descrip,$module->attachs);
		$hireMap = $viewUtility->hireMap();
		$houseFieldsService =	$this->getHouseFieldsService();
		$plate = $houseFieldsService->getCompsiteFieldsByType('plate');
		$paystatus = $houseFieldsService->getCompsiteFieldsByType('paystatus');
		$property = $houseFieldsService->getCompsiteFieldsByType('property');
		$amenities = $houseFieldsService->getCompsiteFieldsByType('amenities');
		$area = $houseFieldsService->getCompsiteFieldsByType('area');
		$equipment = explode(',',$module->equipment);
		$equipmentStr = '';
		foreach($equipment as $value){
			$tmp = $amenities[$value];
			$equipmentStr .= $equipmentStr ? ','. $tmp : $tmp;
		}
		$module->roomname = $viewUtility->buildRoom($module->room);
		$module->platename = $plate[$module->plate];
		$module->fitmentname = $paystatus[$module->fitment];
		$module->equipmentname = $equipmentStr;
		$module->housetypename = $property[$module->housetype];
		$module->hirename = $hireMap[$module->hiretype];
		$module->areatypename = $area[$module->areatype];
		$module->posttime = getLastDate($module->posttime);		
		$module->modifytime = getLastDate($module->modifytime);
		$module->descrip = convert($descrip, $db_windpost);
		return $module;
	}
	
	/**
	 * 取得出租房module
	 *@param int $houseid 房源ID
	 *@param int $ifbuild 是否组装module
	 *@param int $ifuser  是否同时取得出租房发布者信息
	 *@return object;
	 */
	function getHouseHireModule($houseid = 0,$ifbuild = 0,$ifuser = 0){
		$hireService = $this->getHouseHireService();
		$hireModule = $hireService->getHouseHireModule($houseid);
		if($ifbuild){ 
		 	$hireModule = $this->buildHouseHireModule($hireModule);
		}
		if($ifuser){
			$userinfo = $this->getUserInfo($hireModule->uid);
			$hireModule->userinfo = $userinfo;
		}
		return $hireModule;
	}
	
	/**
	 * 取得论坛用户信息
	 *@param int $uid 用户ID
	 *@return array;
	 */
	function getUserInfo($uid){
		require R_P.'require/showimg.php';
		$userService = $this->getUserService();
		$user = $userService->get($uid,true,true);
		$user['faceurl'] = showfacedesign($user['icon'],1,'m'); 
		$regdate = getLastDate($user['regdate']); 
		$user['regdate'] = $regdate[1];
		return $user;
	}
	

	/**
	 * 设置出租房module,设置添加到数据库信息，及相关验证
	 *@param array  	$params    数据
	 *@param boolean 	$ifbroker  是否是经纪人
	 *@param int		$houseid   房源ID
	 *@return object;
	 */
	function setHouseHireModule($params,$ifbroker = false,$houseid=0){
		global $windid;
		$username = $windid;
		if(!S::isArray($params)){
			Error::addError("传入参数有误");
		}
		House::loadClass('househiremodule', 'module', false); /* @var $houseHireModule PW_HouseHireModule */
		$houseHireModule = new PW_HouseHireModule();
		if($houseid){
			$hireService = $this->getHouseHireService();
			$oldHouseHireModule = $hireService->getHouseHireModule($houseid);
			$houseHireModule->attachs = $oldHouseHireModule->attachs;
			$houseHireModule->thumb = $oldHouseHireModule->thumb;
			$houseHireModule->state = $oldHouseHireModule->state;
			$username = $oldHouseHireModule->username;
		}
		$houseHireModule->setSubject($params['subject']);
		$houseHireModule->setUsername($username);
		$houseHireModule->setAcreage($params['acreage']);
		$houseHireModule->setCurrentfloor($params['currentfloor']);
		$houseHireModule->setTotalfloor($params['totalfloor']);
		$houseHireModule->setPrice($params['price']);
		$houseHireModule->setIscharge($params['ischarge']);
		$houseHireModule->setRoom($params['room']);
		$houseHireModule->setAttachs($params['oldatt_desc'],$params['keep']);
		$houseHireModule->setVillage($params['village']);
		$houseHireModule->setPlate($params['plate']);
		$houseHireModule->setAddress($params['address']);
		$houseHireModule->setBuildtime($params['buildtime']);
		$houseHireModule->setTraffic($params['traffic']);
		$houseHireModule->setFitment($params['fitment']);
		$houseHireModule->setEquipment($params['equipment']);
		$houseHireModule->setDescrip($params['descrip']);
		$houseHireModule->setGate($params['gate']);
		$houseHireModule->setPosttime($params['posttime']);
		$houseHireModule->setModifytime($params['modifytime']);
		$houseHireModule->setAreatype($params['areatype']);
		$houseHireModule->setHousetype($params['housetype']);
		$houseHireModule->setHiretype($params['hiretype']);
		$houseHireModule->setLinkman($params['linkman'],$ifbroker);
		$houseHireModule->setTel($params['tel']);
		$houseHireModule->setPhone($params['phone']);
		$houseHireModule->setEmail($params['email']);
		$houseHireModule->setIsbroker($ifbroker);
		$houseHireModule->checkTelOrPhone($ifbroker);
		$houseHireModule->checkFloor();
		Error::checkError();
		return $houseHireModule;
		
	}
	
	/**
	 *添加出租房信息
	 *@param array $params     数据
	 *@param boolean $ifbroker 是否是纪纪人
	 *@return object
	 */
	function addHire($params,$ifbroker = false){
		if(!S::isArray($params)){
			Error::addError("传入参数有误");
		}
		$houseHireModule = $this->setHouseHireModule($params,$ifbroker);
		$hireService = $this->getHouseHireService();
		$houseid = $hireService->addHire($houseHireModule);
		if($houseid){
			$viewUtility= $this->getHouseViewUtility();
			$roomname = $viewUtility->buildRoom($houseHireModule->room);
			$subject = sprintf("[url=%s] %s [/url]", $this->baseUrl . "q=hire&a=houseinfo&houseid=".$houseid,$houseHireModule->subject);
			$content = "房源出租：{$subject}；{$houseHireModule->village}，{$houseHireModule->price}元/月，{$roomname}，{$houseHireModule->address}。";
			$weiboService = $this->getWeiboService();
			$weiboService->send($houseHireModule->uid,$content);
		}
		return $houseHireModule;
	}
	
	/**
	 *编辑出租房信息
	 *@param array $params     数据
	 *@param boolean $ifbroker 是否是纪纪人
	 *@param int $houseid 房源ID
	 *@return object
	 */
	function editHouseHire($params,$ifbroker,$houseid){
		$houseHireModule = $this->setHouseHireModule($params,$ifbroker,$houseid);
		$hireService = $this->getHouseHireService();
		if(($houseHireModule->state == 1 && $params['type'] == 1) || $houseHireModule->state == 2){
			$hireService->republish($houseid);
		}
		$hireService->updateHire($houseHireModule,$houseid);
		return $houseHireModule;
	}
	
	/**
	 * 是否具有发布出租房的权限
	 *@param int $groupid  纪纪人组ID
	 *@param int $ifbroker 是否是经纪人
	 *@param int $ifedit   是否为编辑
	 *@return array
	 */
	function hireRight($groupid = 0,$ifbroker = false,$ifedit = false){
		global $_G,$winduid;
		$secondPostService = $this->getSecondPostService();
		$secondPost = $secondPostService->getSecondPost($winduid);
		$todayNum = (int)$secondPost['todayhirenum'];
		$aleadyNum = (int)$secondPost['hirenum'];
		$totalNum = $remainNum = 0;
		if(!$ifedit){
			if($ifbroker){
				$brokerGroupService = $this->getBrokerGroupService();
				$brokerGroup = $brokerGroupService->getBrokerGroupRight($groupid);
				if(!$brokerGroup['ifhire']) $this->manageError();
				$totalNum = (int)$brokerGroup['hirenum'];
			}else{
				if(!$_G['house_ifhire']) $this->manageError();
				$totalNum = (int)$_G['house_hirenum'];
			}
			$tips = "您今日已经发布<em>{$todayNum}</em>套出租房源";
			if($totalNum > 0){
				$remainNum = $totalNum-$todayNum;
				if($remainNum <= 0) Error::addError("您今天发布出租房源数量超过限制（每天最多{$totalNum}套），请明天再来发布。");
				$tips .= "，您还可以发布<em>{$remainNum}</em>套出租房源。";
			}
			if($totalNum < 0){
				$this->manageError();
			}
		}
		return $tips;
	}
	
	/**
	 *初始化模板中使用的参数
	 *@param  array  $info 
	 *@pram   string $from
	 *@param  int    $houseid
	 *@return object 返回对象化的参数
	 */
	 
	function initHireViewParams(&$info,$from,$houseid){
		global $winduid,$db_attachnum,$db_allowupload,$stylepath,$_G;
		$_viewer = new stdclass();
		$brokerService = $this->getBrokerService();
		$_viewer->houseHireModule = $this->getHouseHireModule($houseid);
		$uid = intval($houseid) ?$_viewer->houseHireModule->uid : $winduid;
		$_viewer->brokerModule = $brokerService->getBrokerModule($uid);
		$_viewer->attachnum = $db_attachnum;
		$_viewer->allowupload = $db_allowupload;
		$_viewer->stylepath = $stylepath;
		$_viewer->_G = $_G;
		$_viewer->baseUrl = $this->baseUrl.'q=manage&';
		$_viewer->url = $_viewer->baseUrl."a=addhire";
		$_viewer->from = $from;
		$_viewer->ifbroker = ($_viewer->brokerModule->uid &&  $_viewer->brokerModule->ifactive) ? true : false;
		$_viewer->tips = $this->hireRight($_viewer->brokerModule->groupid,$_viewer->ifbroker,$houseid);
		if($houseid){
			list($info['oldatt_desc'],$info['keep']) = GP(array('oldatt_desc','keep'));
			$_viewer->url .= '&houseid='.$houseid;
		}else{
			Error::checkError();
		};
		return $_viewer;
	}
	
	/**
	 *发布/编辑出租返回url组装
	 *@param string $from 来源
	 *@param int $houseid 房源ID
	 *@return string
	 */
	function getHireFromUrl($from ='',$houseid=0){
		$url = BASEURL;
		if($from == 'info' && $houseid){
			$url .= 'q=hire&a=houseinfo&houseid='.$houseid;
		}elseif($from == 'publish'){
			$url .= 'q=manage&a=hire&type=publish';
		}elseif($from == 'prepare'){
			$url .= 'q=manage&a=hire&type=prepare';
		}elseif($from == 'expired'){
			$url .= 'q=manage&a=hire&type=expired';
		}else{
			$url .= 'q=manage&a=hire';
		}
		return $url;
	}
	
	/**
	 *出租房seo设置
	 *@param int 	$seopage  read/index/thread三种类别
	 *@param string $name     房源名称
	 *@param int    $area	    房源所在区域
	 *@param string $vallage 小区名称
	 *@return array
	 */
	function setSeoParams($seopage='index',$name = '',$area = 0,$village = ''){
		global $db_bbsname;
		include R_P.'/data/bbscache/house_config.php';
		$seopage = $seopage ? $seopage : 'index';
		$title = $keywords = $descript = $areaname = '';
		if($seopage == 'index' || $seopage == 'thread'){
			foreach($house_field['area'] as $key => $value){
				$areaname .= $areaname ? ','.$value['name'].'租房' : $value['name'].'租房';
			}
			$title = $db_bbsname;
			$keywords = "{$house_mapcity}租房,{$areaname},租房，{$db_bbsname}";
			$descript = "{$house_mapcity}租房";
		}elseif($seopage == 'read'){
			$areaname = $house_field['area'][$area]['name'];
			$title = $name.'-'.$db_bbsname;
			$keywords = "{$village},{$areaname}租房，{$db_bbsname}";
			$descript = "{$village}租房,{$name}";
		}
		$defaultSeo = array('title' => $title , 'metaDescription' => $descript, 'metaKeywords' => $keywords);
		return array($seopage,$defaultSeo,$name);
	}
	
}